package com.yitao.cms.service.user.cmsUser;

import com.skipper.base.service.GenericEntityService;
import com.yitao.cms.dto.operationUser.OperationUserRoleDto;
import com.yitao.cms.model.operationUser.OperationUserRoleModel;
import com.yitao.cms.model.operationUser.QOperationUserRoleModel;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.transaction.Transactional;

/**
 * @Auther: wwl
 * @Date: 2019/3/12 0012 10:27
 * @Description:
 */
@Service
public class OperationUserRoleService extends GenericEntityService<OperationUserRoleModel, OperationUserRoleDto, QOperationUserRoleModel> {
    public OperationUserRoleService(EntityManager entityManager){
        super(entityManager);
    }
    /**
     * 添加用户的角色
     *
     * @param userId
     * @param
     */
    @Transactional
    public OperationUserRoleModel updateUserRole(Long userId, Long roleId) {
        if (userId!=null && roleId!=null){
            repository.deleteByWhere(Q.userId.eq(userId));
            OperationUserRoleModel userRoleModel = new OperationUserRoleModel();
            userRoleModel.setRoleId(roleId);
            userRoleModel.setUserId(userId);
            return  repository.save(userRoleModel);
        }
       return null;
    }

}
